﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using Konekcija.Core.Reporting.Dto.Application.Content;

namespace Konekcija.Core.Reporting.Mappings.Content
{
    public class ItemMap : ClassMap<Item>
    {
        public ItemMap()
        {
            Schema("content");
            Table("Items");

            Id(x => x.Id).GeneratedBy.GuidComb().Not.Nullable();
            Map(x => x.RegionId).Column("RegionId")
                .Not.Insert().Not.Update().Nullable();
            Map(x => x.DirectionId).Column("DirectionId")
                .Not.Insert().Not.Update().Nullable();
            Map(x => x.StatusId).Column("StatusId")
                .Not.Insert().Not.Update().Nullable();
            Map(x => x.ConditionId).Column("ConditionId")
                .Not.Insert().Not.Update().Nullable();
            Map(x => x.ItemPriceOptionId).Column("ItemPriceOptionId")
                .Not.Insert().Not.Update().Nullable();
            Map(x => x.HasPrice).Nullable();
            Map(x => x.HasAuction).Nullable();
            Map(x => x.IsSaved).Nullable();
            Map(x => x.PreviewImageId).Nullable();
            Map(x => x.CountBidding).Nullable();
            Map(x => x.CountPhotos).Nullable();
            Map(x => x.CountVideos).Nullable();
            Map(x => x.CountAudio).Nullable();
            Map(x => x.CountFiles).Nullable();
            Map(x => x.CountComments).Nullable();
            Map(x => x.CountView).Nullable();
            Map(x => x.Votes).Nullable();
            Map(x => x.TotalRating).Nullable();
            Map(x => x.CommentsEnabled).Nullable();
            Map(x => x.OnlyForMembers).Nullable();
            Map(x => x.Url).Nullable();
            Map(x => x.DateCreated).Nullable();
            Map(x => x.DateChanged).Nullable();
            Map(x => x.DateStart).Nullable();
            Map(x => x.DateEnd).Nullable();
            Map(x => x.ByUserId).Nullable();

            // content
            HasMany(x => x.ItemLanguages)
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId")
                .Fetch.Join()
                .Cascade.AllDeleteOrphan();

            HasMany(x => x.Comments)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId");

            HasMany(x => x.SavedBy)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId");

            HasMany(x => x.Auctions)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId");

            HasMany(x => x.ItemPriceVariants)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId");

            References(x => x.ItemPriceOption).Nullable();

            // files
            HasMany(x => x.Audios)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId")
                .Cascade.AllDeleteOrphan();

            HasMany(x => x.Videos)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId")
                .Cascade.AllDeleteOrphan();

            HasMany(x => x.Pictures)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId")
                .Cascade.AllDeleteOrphan();

            HasMany(x => x.Documents)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Key(k => k.Not.Nullable())
                .KeyColumn("ItemId")
                .Cascade.AllDeleteOrphan();

            // configurations
            HasManyToMany(x => x.Categories)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("ItemId")
                .ChildKeyColumn("CategoryId")
                .Table("content.ItemsToCategories");


            HasManyToMany(x => x.Navigations)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("ItemId")
                .ChildKeyColumn("NavigationId")
                .Table("content.ItemsToNavigations");

            References(x => x.Region).Nullable();

            // meta
            References(x => x.Status).Nullable();
            References(x => x.Condition).Nullable();
            References(x => x.Direction).Nullable();

            HasManyToMany(x => x.Levels)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("ItemId")
                .ChildKeyColumn("LevelId")
                .Table("content.ItemsToLevels");

            HasManyToMany(x => x.Types)
                .LazyLoad()
                .Inverse()
                .AsBag()
                .Cascade.None()
                .ParentKeyColumn("ItemId")
                .ChildKeyColumn("TypeId")
                .Table("content.ItemsToTypes");
        }
    }
}
